/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server;

import communicate_interface.ClientInterface;
import communicate_interface.ServerInterface;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import util.Database;

/**
 *
 * @author senhlyheng
 */
public class Server implements ServerInterface{

    private ArrayList<ClientInterface> clients = new ArrayList<ClientInterface>();

    @Override
    public TodoItem getTodo(int id) {
        return Database.getTodoItem(id);

    }

    @Override
    public List<TodoItem> getList(ClientInterface source) {
//        System.out.println("source="+source);
        if (!clients.contains(source)) {
//            System.out.println("helo source");
            clients.add(source);
        }
        return Database.getList();

    }

    @Override
    public void disconnect(ClientInterface source) {
        clients.remove(source);
    }

    @Override
    public TodoItem updateTodo(TodoItem item, ClientInterface source) {
//        System.out.println("id="+item.getId());
        int id = item.getId();
        int version = item.getVersion();

        if (id != -1) {
            TodoItem localitem = getTodo(id);
            if (localitem == null || localitem.getVersion() != version) {
                return null;
            }
        }

        if (id == -1) {
            item.setVersion(1);
            Database.insertTodo(item);
//            System.out.println("insert");
        } else {
            item.setVersion(++version);
            Database.updateTodo(item, id);
//            System.out.println("update");
        }

        if (!clients.contains(source)) {
            clients.add(source);
        }

//        System.out.println("size"+clients.size());
        for (ClientInterface client : clients) {
//            System.out.println("client==null"+(client==null));
            if (!client.equals(source)) {
                try {
//                    System.out.println("notify");
                    client.notifyUpdate();
                } catch (RemoteException ex) {
                    Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        return item;


    }

    @Override
    public boolean deleteTodo(TodoItem item, ClientInterface source) {

        int id = item.getId();
        int version = item.getVersion();
        
        TodoItem localitem = getTodo(id);

        if (localitem != null) {
            if (localitem.getVersion() != version) {
                return false;
            }
        }
        else return true;
        
        Database.deleteTodo(id);
        
        if (!clients.contains(source)) {
            clients.add(source);
        }

        for (ClientInterface client : clients) {
            if (!client.equals(source)) {
                try {
                    client.notifyUpdate();
                } catch (RemoteException ex) {
                    Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        return true;
    }
}
